'Option Explicit
Sub main()

Dim url As String
Dim tagName As String

url = ActiveSheet.Range("B1").Value
tagName = ActiveSheet.Range("B2").Value

Dim IE As Object
Set IE = GetInternetWindow(url)

If IE Is Nothing Then
 MsgBox "URL Does not Match any IE window currently opened. Please check again.", vbCritical
 Exit Sub
 
Else
 getCSSSelectors IE, tagName
End If
MsgBox "Done"

End Sub


Function GetInternetWindow(urlToLookFor As String) As Object

'Returns the IE object if the URL is found or else null
Set GetInternetWindow = Nothing
Set winShell = CreateObject("Shell.Application")
Dim IE As Object
For Each IE In winShell.Windows
    'MsgBox ie.LocationURL
    If IE.LocationURL Like "*" & urlToLookFor & "*" Then
        Set GetInternetWindow = IE
        Exit For
    End If
Next

End Function

Function skipTag(tagName As String) As Boolean
 skipTag = False
 If (UCase(tagName) = "HTML" Or _
     UCase(tagName) = "BODY" Or _
     UCase(tagName) = "SCRIPT" Or _
     UCase(tagName) = "HEAD" Or _
     UCase(tagName) = "META") Then
   skipTag = True
 End If
End Function


Sub getCSSSelectors(IE As Object, htmltag As String)
Dim CSSSelector As String
Dim printLoc As Integer
printLoc = ActiveSheet.UsedRange.Rows.Count + 1
'MsgBox printLoc
Set objs = IE.Document.getElementsByTagName(htmltag)

If objs.Length < 1 Then
  MsgBox "No Elements found with this Tag."
  Exit Sub
End If

For i = 0 To objs.Length - 1
 If (Not skipTag(objs.Item(i).tagName)) Then
    CSSSelector = "css==" & objs.Item(i).tagName
    Set attr = objs.Item(i).Attributes
    If Not attr Is Nothing Then
       For Each a In attr
          If (a.specified) Then
             CSSSelector = CSSSelector & "[" & a.nodeName & "='" & a.NodeValue & "']"
           End If
        Next
     'Print only non hidden elements
        If Not CSSSelector Like "*" & "type='hidden'" & "*" Then
            If CSSSelector Like "*" & "='" & "*" Then
               ActiveSheet.Cells(printLoc, 1).Value = CSSSelector
               ActiveSheet.Cells(printLoc, 2).Value = objs.Item(i).innerText
                printLoc = printLoc + 1
             End If
        End If
     End If
 End If

Next i

End Sub

